package com.example.cephdemo.demos.web;

import com.example.cephdemo.demos.utils.MinioUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("store")
@RequiredArgsConstructor
public class StorageController {

    @PostMapping("upload")
    public String upload(@RequestParam MultipartFile file, @RequestParam String bucket) {
        try {
            if (!MinioUtil.bucketExists(bucket)) {
                MinioUtil.createBucket(bucket);
            }
            MinioUtil.uploadFile(bucket, file,file.getOriginalFilename());
            return MinioUtil.getPreSignedObjectUrl(bucket, file.getOriginalFilename());
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }
}
